// pages/index/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    complete_f: "待完成",
    complete_t: "已完成",
    title_top: "进行中",
    title_buttom: "已完成",
    array: [],
    displayArray: [],
    searchValue: ""
  },

  //加号按钮绑定
  handleAdd(e) {
    wx.navigateTo({
      url: '/pages/add/add',
    })
  },

  //复选框绑定
  handleCheckboxTap(e) {
    const currentId = e.currentTarget.dataset.id;
    // 2. 遍历数组，找到对应id的项并更新状态
    const updatedArray = this.data.array.map(item => {
      if (item.id === currentId) {
        // 切换complete状态（true ↔ false）
        item.complete = !item.complete;
        // 根据新的complete状态更新text文本
        item.text = item.complete ? this.data.complete_t : this.data.complete_f;
      }
      return item;
    });
    // 3. 更新数据，视图会自动同步
    this.setData({
      array: updatedArray,
      displayArray: updatedArray
    });
    wx.setStorageSync("todoArray", updatedArray);
  },
  onInput: function (e) {
    this.setData({
      searchValue: e.detail.value
    })
  },
  onSearch() {
    const { searchValue, array } = this.data;
    if (!searchValue.trim()) {
      //trim()去除空格 
      // 无关键词时，展示所有数据
      this.setData({ 
        displayArray: array 
      });
      return;
    }
    // 根据标题模糊搜索
    const filtered = array.filter((item) =>
      item.title.includes(searchValue)
    );
    this.setData({ displayArray: filtered });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    const cachedArray = wx.getStorageSync("todoArray");
    if (cachedArray && cachedArray.length) {
      this.setData({
        array: cachedArray,
        displayArray: cachedArray,
      });
    } else {
      this.setData({
        array: [
          {
            id: 1,
            title: "待办事件1",
            text: this.data.complete_f,
            complete: false,
            detial_text:"事件描述1",
            deadline:"2025-10-21"
          },
          {
            id: 2,
            title: "待办事件2",
            text: this.data.complete_f,
            complete: false,
            detial_text:"事件描述2",
            deadline:"2025-10-21"
          },
          {
            id: 3,
            title: "待办事件3",
            text: this.data.complete_t,
            complete: true,
            detial_text:"事件描述3",
            deadline:"2025-10-21"
          },
          {
            id: 4,
            title: "待办事件4",
            text: this.data.complete_t,
            complete: true,
            detial_text:"事件描述4",
            deadline:"2025-10-21"
          },
          {
            id: 5,
            title: "待办事件5",
            text: this.data.complete_t,
            complete: true,
            detial_text:"事件描述5",
            deadline:"2025-10-21"
          }
        ],
        displayArray: [
          {
            id: 1,
            title: "待办事件1",
            text: this.data.complete_f,
            complete: false,
            detial_text:"事件描述1",
            deadline:"2025-10-21"
          },
          {
            id: 2,
            title: "待办事件2",
            text: this.data.complete_f,
            complete: false,
            detial_text:"事件描述2",
            deadline:"2025-10-21"
          },
          {
            id: 3,
            title: "待办事件3",
            text: this.data.complete_t,
            complete: true,
            detial_text:"事件描述3",
            deadline:"2025-10-21"
          },
          {
            id: 4,
            title: "待办事件4",
            text: this.data.complete_t,
            complete: true,
            detial_text:"事件描述4",
            deadline:"2025-10-21"
          },
          {
            id: 5,
            title: "待办事件5",
            text: this.data.complete_t,
            complete: true,
            detial_text:"事件描述5",
            deadline:"2025-10-21"
          },
        ]
      })
      wx.setStorageSync("todoArray", this.data.array);
    }

    wx.setNavigationBarTitle({
      title: '我的待办'
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const updatedArray = wx.getStorageSync("todoArray");
    if (updatedArray) {
      this.setData({
        array: updatedArray,
        displayArray: updatedArray,
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

})